package com.itheima.web;

import com.itheima.pojo.User;
import com.itheima.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private UserService service = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1。获取用户名和密码
        String username = request.getParameter("username");
        String password=request.getParameter("password");

        // 获取复选框的数据信息
        String remember = request.getParameter("remember");

        //2. 调用servlet进行查询结构
        User user = service.login(username, password);

        // 3. 对当前的输入进行判断
        if(user!=null){
            // 登陆成功，此时返回BrandServlet页面
            // 判断此时的用户是否勾选了记住我的选项
            if("1".equals(remember)) {
                // 如果此时勾选了记住我的选项
                // 1.创建cookie的对象,对客户端来的Cookie数据进行记录
                Cookie cookie_username = new Cookie("username", username);
                Cookie cookie_password = new Cookie("password", password);

                // 设置好Cookie的存活时间
                cookie_username.setMaxAge(60*60*24*7); // 一周的时间
                cookie_password.setMaxAge(60*60*24*7); //一周时间
                // 2. 发送给客户端浏览器
                response.addCookie(cookie_username);
                response.addCookie(cookie_password);

            }
            // 将此时的登陆成功的user对象记录在服务器端的Session中
            HttpSession session = request.getSession();
            session.setAttribute("user",user);

            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath+"/selectAllServlet");
        }else{
            // 登陆失败的话

            // 存储错误的信息到request
            // 存储错误信息到request
            request.setAttribute("login_msg","用户名或密码错误");

            // 跳转到login.jsp
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }

       /* //1. 获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //获取复选框数据
        String remember = request.getParameter("remember");

        //2. 调用service查询
        User user = service.login(username, password);

        //3. 判断
        if(user != null){
            //登录成功，跳转到查询所有的BrandServlet

            //判断用户是否勾选记住我
            if("1".equals(remember)){
                //勾选了，发送Cookie

                //1. 创建Cookie对象
                Cookie c_username = new Cookie("username",username);
                Cookie c_password = new Cookie("password",password);
                // 设置Cookie的存活时间
                c_username.setMaxAge( 60 * 60 * 24 * 7);
                c_password.setMaxAge( 60 * 60 * 24 * 7);
                //2. 发送
                response.addCookie(c_username);
                response.addCookie(c_password);
            }

            //将登陆成功后的user对象，存储到session
            HttpSession session = request.getSession();
            session.setAttribute("user",user);

            String contextPath = request.getContextPath();
            response.sendRedirect(contextPath+"/selectAllServlet");
        }else {
            // 登录失败,

            // 存储错误信息到request
            request.setAttribute("login_msg","用户名或密码错误");

            // 跳转到login.jsp
            request.getRequestDispatcher("/login.jsp").forward(request,response);

        }*/
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}